package com.example.permissionsystem1_1.businessFunction.service.interfaces.impl;

import com.example.permissionsystem1_1.businessFunction.dao.entity.po.*;
import com.example.permissionsystem1_1.businessFunction.dao.repository.*;
import com.example.permissionsystem1_1.businessFunction.service.converter.DepartmentUserServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.converter.UserRoleServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.departmentUserReqModel.DepartmentUserSelectAllDepartmentsByUserIdReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.departmentUserReqModel.DepartmentUserSetDepartmentUserRelationReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.userRoleReqModel.UserRoleSelectAllRolesByUserIdReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.userRoleReqModel.UserRoleSetUserRoleRelationReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.departmentUserRspModel.DepartmentUserSelectAllDepartmentsByUserIdRspModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.departmentUserRspModel.DepartmentUserSetDepartmentUserRelationRspModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.userRoleRspModel.UserRoleSelectAllRolesByUserIdRspModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.userRoleRspModel.UserRoleSetUserRoleRelationRspModel;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.DepartmentUserService;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.UserRoleService;
import com.example.permissionsystem1_1.common.businessFunction.BaseServiceImpl;
import com.example.permissionsystem1_1.system.annotation.aspectAnnotation.SuperManageOperationAspectAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserRoleServiceImpl extends BaseServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private RoleInfoDao roleInfoDao;
    @Autowired
    private UserFunctionRecordDao userFunctionRecordDao;
    @Autowired
    private RoleFunctionDao roleFunctionDao;

    @Override
    @SuperManageOperationAspectAnnotation
    public UserRoleSelectAllRolesByUserIdRspModel selectAllDepartmentsByUserId(UserRoleSelectAllRolesByUserIdReqModel reqModel) {
        UserRolePo po= UserRoleServiceConverter.convertReqModelToPo(reqModel);
        List<UserRolePo> pos=null;
        if(po!=null&&po.getUserId()!=null){
            pos=userRoleDao.selectAllRolesByUserId(po.getUserId());
        }
        return UserRoleServiceConverter.covertToSelectAllRolesByUserIdModel(pos);
    }

    @Override
    @Transactional
    @SuperManageOperationAspectAnnotation
    public UserRoleSetUserRoleRelationRspModel setUserRoleRelation(UserRoleSetUserRoleRelationReqModel reqModel) {
        List<UserRolePo> pos=UserRoleServiceConverter.convertReqModelToPo(reqModel);
        if(pos==null||pos.size()<=0){
            return null;
        }
        Long userId=pos.get(0).getUserId();
        UserRolePo userRolePo=new UserRolePo();
        userRolePo.setUserId(userId);
        int i1=userRoleDao.deletes(userRolePo);//无论是否有这个用户,都进行一下数据清理,以便保证
        UserInfoPo userInfoPo=userInfoDao.selectById(userId);
        List<RoleInfoPo> roleInfoPos=roleInfoDao.selectBatchByIds(reqModel.getRoleIds());
        if(userInfoPo==null||roleInfoPos==null||roleInfoPos.size()<=0){
            //仅仅做了删除原始关联,而没有对指定用户添加任何角色
            return UserRoleServiceConverter.covertToSetUserRoleRelationModel(i1);
        }
        List<Long> roleIds=new ArrayList<>();//这里存的是查询后此时真实存在的角色id
        List<UserRolePo> existPos=new ArrayList<>();//因为可能存在对象数量的减少,所以需要另外创建一个存对象的集合才行
        Date now=new Date();
        for (RoleInfoPo roleInfoPo:roleInfoPos) {
            Long roleId=roleInfoPo.getId();
            roleIds.add(roleId);
            UserRolePo existPo=new UserRolePo();
            existPo.setUserId(userId);
            existPo.setRoleId(roleId);
            existPo.setState("0");
            existPo.setCreator("admin");
            existPo.setCreateTime(now);
            existPo.setUpdator("admin");
            existPo.setUpdateTime(now);
            existPos.add(existPo);
        }
        int i=-1;
        int i2=userRoleDao.insertBatch(existPos);
        i=i1+i2;
        //功能权限的重建:根据用户id对指定用户进行功能权限的重建
        reconstructionUserFunctionsByUserIdAndRoleIds(userId,roleIds);

        return UserRoleServiceConverter.covertToSetUserRoleRelationModel(i);
    }

    //**********本类的业务处理方法
    /** 功能权限的重建:根据用户id和角色id集合来重建用户功能 */
    private void reconstructionUserFunctionsByUserIdAndRoleIds(Long userId,List<Long> roleIds){
        UserFunctionRecordPo userFunctionRecordPo=new UserFunctionRecordPo();
        userFunctionRecordPo.setUserId(userId);
        userFunctionRecordDao.deletes(userFunctionRecordPo);

        List<RoleFunctionPo> roleFunctionPos=roleFunctionDao.selectAllFunctionsByRoleIds(roleIds);
        List<UserFunctionRecordPo> userFunctionRecordPos=new ArrayList<>();
        String creator="admin";
        String updator="admin";
        Date now=new Date();
        for (RoleFunctionPo roleFunctionPo:roleFunctionPos) {
            UserFunctionRecordPo ufrPo=new UserFunctionRecordPo();
            ufrPo.setUserId(userId);
            ufrPo.setFunctionId(roleFunctionPo.getFunctionId());
            ufrPo.setCreator(creator);
            ufrPo.setCreateTime(now);
            ufrPo.setUpdator(updator);
            ufrPo.setUpdateTime(now);
            userFunctionRecordPos.add(ufrPo);
        }
        userFunctionRecordDao.insertBatch(userFunctionRecordPos);
    }
}
